package com.project.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.User;
import com.project.exception.CustomException;
import com.project.mapper.UserMapper;
import com.project.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @Date: 2022/2/17 15:15
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return 用户
     */
    @Override
    public User login(String username, String password) {
        User user = this.selectByUsername(username);//根据用户名查找用户
        if (Objects.isNull(user)) {//未找到
            throw new CustomException(400,"用户名错误");
        }
        if (!user.getPassword().equals(password)){//判断数据库中存的密码与用户输的密码是否一致
            throw new CustomException(400,"密码错误");
        }
        user.setLoginTime(new Date());
        userMapper.updateById(user);
        return user;
    }

    /**
     * 注册
     * @param username 用户名
     * @param realName 真实姓名
     * @param password 密码
     * @param phone 手机号
     * @param grade 级别
     */
    @Override
    public void register(String username, String realName, String password, String phone, String grade) {
        // 首先检查此用户是否在数据库
        if (this.selectByUsername(username) != null) {
            throw new CustomException(400, "该用户名已经注册");
        }
        // 将用户插入user表中
        User user = new User();
        user.setUsername(username);
        user.setRealName(realName);
        user.setPassword(password);
        user.setPhone(phone);
        user.setRole("common");
        user.setGrade(grade);
        user.setRegisterTime(new Date());
        userMapper.insert(user);
    }

    /**
     * 修改用户密码
     * @param id 用户id
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     */
    @Override
    public void changePwd(Long id, String oldPassword, String newPassword) {
        User user = userMapper.selectById(id);//根据id查找用户
        if (user == null) {
            throw new CustomException(400,"用户不存在");
        }

        if (!user.getPassword().equals(oldPassword)){//数据库中存的密码与输入的旧密码不一致
            throw new CustomException(400,"旧密码错误");
        }

        if (oldPassword.equals(newPassword)){
            throw new CustomException(400,"新旧密码不能一样");
        }

        user.setPassword(newPassword);
        user.setUpdateTime(new Date());
        userMapper.updateById(user);
    }

    /**
     * 分页查询用户列表
     * @param page 第几页
     * @param pageSize 每页数据量
     * @param username 用户名
     * @return 分页结果
     */
    @Override
    public IPage<User> selectPageUserList(Long page, Long pageSize, String username) {
        IPage<User> userIPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(username),User::getUsername,username);
        return userMapper.selectPage(userIPage,wrapper);
    }

    /**
     * 修改个人信息
     * @param id 用户id
     * @param username 用户名
     * @param realName 姓名
     * @param phone 手机号
     * @param grade 级别
     */
    @Override
    public void update(Long id, String username, String realName, String phone, String grade) {
        User user = userMapper.selectById(id);//根据id查找用户
        if (user == null) {
            throw new CustomException(400,"用户不存在");
        }

        User user1 = this.selectByUsername(username);
        // 检查新的用户的是否在数据库
        if (user1 != null && !user.getId().equals(user1.getId())) {
            throw new CustomException(400, "该用户名已经注册");
        }
        //修改用户信息
        user.setUsername(username);
        user.setRealName(realName);
        user.setPhone(phone);
        user.setGrade(grade);
        user.setUpdateTime(new Date());
        userMapper.updateById(user);
    }

    /**
     * 删除用户
     * @param id 用户id
     */
    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    /**
     * 根据用户id重置密码
     * @param userId 用户id
     */
    @Override
    public void resetPwd(Long userId) {
        User user = userMapper.selectById(userId);//根据id查找用户
        if (user == null) {
            throw new CustomException(400,"用户不存在");
        }
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        userMapper.updateById(user);
    }

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return 用户
     */
    private User selectByUsername(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        return userMapper.selectOne(wrapper);
    }
}
